package com.myacg.editor.common.unit.list.item;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;

import com.myacg.editor.common.unit.list.FlowListPanel;
import com.myacg.editor.common.unit.list.ListEvent;

public abstract class FlowListItem<T> extends JPanel{
	private static final long serialVersionUID = 1L;
	private T data;
    private String id;
    private FlowListPanel<T>  parent ;
    private boolean selected;
    private boolean entered;
    private Color selectColor; 
    private Color defaultColor;
    private Color hoverColor;
    private Color backgroundColor; // 用于绘制的color
    //剥离出去的事件触发机制
  	private ListEvent  listEvent;
	 public FlowListItem(String id, T data) {
		 this.data = data;
		 this.id = id;
		 this.selectColor = new Color(180, 180,180);
		 this.defaultColor = new Color(240, 240, 240);
		 this.hoverColor =  new Color(210, 210,210);
		 this.backgroundColor  = this.defaultColor;
		 //选中事件
		 this.addMouseListener(new MouseAdapter() {
			 @Override
			public void mousePressed(MouseEvent e) {
				super.mousePressed(e);
				//选中组件本身后 通知父组件选中的id
				parent.setSelectId(FlowListItem.this.id);
			}
			 @Override
			public void mouseEntered(MouseEvent e) {
				super.mouseEntered(e);
				FlowListItem.this.setEntered(true);
				setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
			}
			 @Override
			public void mouseExited(MouseEvent e) {
				super.mouseExited(e);
				FlowListItem.this.setEntered(false);
				setCursor( Cursor.getDefaultCursor());
			}
			 
		});
	 }
	 /**
	  * 重绘
	  */
	 @Override
	 protected void paintComponent(Graphics g) {
			super.paintComponent(g);
			//提供基本的绘制颜色的方法
			chageColorState();
			g.setColor(backgroundColor);
			g.fillRect(0, 0, getWidth(), getHeight());
			//通用绘制方法
			drawContent(g, data);
	 }  
	/**
	 * 基于T数据类型data的绘制 
	 * @param g
	 * @param data
	 */
	public abstract void drawContent(Graphics g, T data);
	
	//选择状态引起的颜色变化
	private  void chageColorState(){
		//变更颜色
			if(this.entered){
				if(this.selected){
					this.backgroundColor = this.selectColor;
				}else{
					this.backgroundColor = this.hoverColor;
				}
			}else{
				if(this.selected){
					this.backgroundColor = this.selectColor;
				}else{
					this.backgroundColor = this.defaultColor;
				}
			}
	}
	
	public T getData() {
		return data;
	}
	public void setData(T data) {
		this.data = data;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public FlowListPanel<T> getParent() {
		return parent;
	}
	public void setParent(FlowListPanel<T> parent) {
		this.parent = parent;
	}
	public boolean isSelected() {
		return selected;
	}
	/*状态发生改变时触发重绘*/
	public void setSelected(boolean selected) {
		this.selected = selected;
		//子组件状态发生改变时回调这个方法
		if(listEvent!=null){
			listEvent.selectChangeEvent(parent, this);
		}
		repaint();
	}
	public boolean isEntered() {
		return entered;
	}
	/*状态发生改变时触发重绘*/
	public void setEntered(boolean entered) {
		this.entered = entered;
		repaint();
		if(listEvent!=null){
			listEvent.hoverChangeEvent(parent, this);
		}
	}
	public Color getSelectColor() {
		return selectColor;
	}
	public void setSelectColor(Color selectColor) {
		this.selectColor = selectColor;
	}
	public Color getDefaultColor() {
		return defaultColor;
	}
	public void setDefaultColor(Color defaultColor) {
		this.defaultColor = defaultColor;
	}
	public Color getHoverColor() {
		return hoverColor;
	}
	public void setHoverColor(Color hoverColor) {
		this.hoverColor = hoverColor;
	}
	public ListEvent getListEvent() {
		return listEvent;
	}
	public void setListEvent(ListEvent listEvent) {
		this.listEvent = listEvent;
	}
 
	
	
	 
}
